/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Dao.AlunoDao;
import Dao.AlunoDaoImplJPA;
import Dao.ProfessorDao;
import Dao.ProfessorDaoImplJPA;
import Pojo.Aluno;
import Pojo.Professor;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author thiago
 */
public class FormAcesso extends javax.swing.JFrame {

    /**
     * Creates new form FormAcesso
     */
    public FormAcesso() {
        initComponents();
        cbbAluno.setVisible(false);
        cbbProfessor.setVisible(false);
        rbtAdministrador.setSelected(true);
        this.buscarAlunos();
        this.buscarProfessores();
    }
    
    /**
     * Cria a lista de alunos no comboBox
     */
    private void buscarAlunos(){
        AlunoDao alunoDao = new AlunoDaoImplJPA();
        List<Aluno> listaAlunos = alunoDao.pesquisarTodos();
        for(Aluno aluno : listaAlunos){
            cbbAluno.addItem(aluno.getId()+" - "+aluno.getNome());
        }
    }
    
    /**
     * Cria a lusta de professores no comboBox
     */
    private void buscarProfessores(){
        ProfessorDao professorDao = new ProfessorDaoImplJPA();
        List<Professor> listaProfessores = professorDao.pesquisarTodos();
        for(Professor professor : listaProfessores){
            cbbProfessor.addItem(professor.getId()+" - "+professor.getNome());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        panelAcesso = new javax.swing.JPanel();
        rbtAdministrador = new javax.swing.JRadioButton();
        rbtProfessor = new javax.swing.JRadioButton();
        rbtAluno = new javax.swing.JRadioButton();
        cbbProfessor = new javax.swing.JComboBox();
        cbbAluno = new javax.swing.JComboBox();
        btnLogin = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LOGIN");
        setBounds(new java.awt.Rectangle(500, 200, 0, 0));

        panelAcesso.setBorder(javax.swing.BorderFactory.createTitledBorder("Forma de Acesso"));

        buttonGroup.add(rbtAdministrador);
        rbtAdministrador.setText("Administrador");
        rbtAdministrador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtAdministradorActionPerformed(evt);
            }
        });

        buttonGroup.add(rbtProfessor);
        rbtProfessor.setText("Professor");
        rbtProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtProfessorActionPerformed(evt);
            }
        });

        buttonGroup.add(rbtAluno);
        rbtAluno.setText("Aluno");
        rbtAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtAlunoActionPerformed(evt);
            }
        });

        btnLogin.setText("Log in");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelAcessoLayout = new javax.swing.GroupLayout(panelAcesso);
        panelAcesso.setLayout(panelAcessoLayout);
        panelAcessoLayout.setHorizontalGroup(
            panelAcessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAcessoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelAcessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(rbtAdministrador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(rbtAluno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(rbtProfessor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAcessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelAcessoLayout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(btnLogin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSair, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                    .addComponent(cbbAluno, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbbProfessor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelAcessoLayout.setVerticalGroup(
            panelAcessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAcessoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelAcessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rbtProfessor))
                .addGap(18, 18, 18)
                .addGroup(panelAcessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbtAluno)
                    .addComponent(cbbAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panelAcessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbtAdministrador)
                    .addComponent(btnLogin)
                    .addComponent(btnSair))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelAcesso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelAcesso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        // TODO add your handling code here:
        System.exit(WIDTH);
    }//GEN-LAST:event_btnSairActionPerformed

    private void rbtProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtProfessorActionPerformed
        // TODO add your handling code here:
        cbbProfessor.setVisible(true);
        cbbAluno.setVisible(false);
    }//GEN-LAST:event_rbtProfessorActionPerformed

    private void rbtAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtAlunoActionPerformed
        // TODO add your handling code here:
        cbbProfessor.setVisible(false);
        cbbAluno.setVisible(true);
    }//GEN-LAST:event_rbtAlunoActionPerformed

    private void rbtAdministradorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtAdministradorActionPerformed
        // TODO add your handling code here:
        cbbProfessor.setVisible(false);
        cbbAluno.setVisible(false);
    }//GEN-LAST:event_rbtAdministradorActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
      
        int acesso;     // 1 = administrador, 2 = professor, 3 = aluno
        String professorString;
        String alunoString;
        int professorId;
        int alunoId;
        String[] dados;
        Aluno aluno = null;
        Professor professor = null;
        AlunoDao alunoDao = new AlunoDaoImplJPA();
        ProfessorDao professorDao = new ProfessorDaoImplJPA();
        
        if (rbtAdministrador.isSelected()){
            acesso = 1;
            new Desktop(acesso,null).setVisible(true);
            this.setVisible(false);
        }
        
        if (rbtProfessor.isSelected()){
            acesso = 2;
            professorString = (String) cbbProfessor.getSelectedItem();
            if (professorString != null){
                dados = professorString.split(" - ");
                professorId = Integer.parseInt(dados[0]);
                professor = professorDao.pesquisarPorId(professorId);
                new Desktop(acesso,professor).setVisible(true);
                this.setVisible(false);
            }else{
                JOptionPane.showMessageDialog(null, "Impossível logar como Professor. Nenhum cadastrado.");
            }
        }
        
        if (rbtAluno.isSelected()){
            acesso = 3;
            alunoString = (String) cbbAluno.getSelectedItem();
            if (alunoString != null){
                dados = alunoString.split(" - ");
                alunoId = Integer.parseInt(dados[0]);
                aluno = alunoDao.pesquisarPorId(alunoId);
                new Desktop(acesso,aluno).setVisible(true);   
                this.setVisible(false);
            }else{
                JOptionPane.showMessageDialog(null, "Impossível logar como Aluno. Nenhum cadastrado.");
            }
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormAcesso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormAcesso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormAcesso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormAcesso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormAcesso().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnSair;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JComboBox cbbAluno;
    private javax.swing.JComboBox cbbProfessor;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel panelAcesso;
    private javax.swing.JRadioButton rbtAdministrador;
    private javax.swing.JRadioButton rbtAluno;
    private javax.swing.JRadioButton rbtProfessor;
    // End of variables declaration//GEN-END:variables
}
